﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using WPS.Log;
using WPS.SDK.exception;

namespace WPS.SDK.http
{
    public class Response
    {
        private HttpWebResponse _response;
        public Response(HttpWebResponse response)
        {
            _response = response;
        }

        public string getHeader(String header)
        {
            return this._response.Headers.ToString();
        }
        private static void parseXML(HttpWebResponse response)
        {
            String xmlString = null;
            try
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    xmlString= reader.ReadToEnd();
                }
            }
            catch (IOException e)
            {
              //  parseIOException(e);
            }
            if (xmlString.StartsWith("<?xml"))
            {
                int fromIndex = xmlString.IndexOf("<Code>");
                if (fromIndex != -1)
                {
                    fromIndex += 6;
                    int toIndex = xmlString.IndexOf("</Code>", fromIndex);
                    if (toIndex != -1)
                    {
                       // throw new YunXmlException(xmlString.Substring(fromIndex, toIndex), xmlString, response.c());
                    }
                }
              //  throw new YunXmlException(null, xmlString, response.code());
            }
            if (xmlString.StartsWith("<html>"))
            {
             //   throw new YunHtmlException(xmlString, response.code());
            }
        }

        private static JObject parseJSON(HttpWebResponse response)
        {
            String jsonString = null;
            try
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    jsonString = reader.ReadToEnd();
                    LogHelper.Info("response :" + jsonString);
                }
            }
            catch (IOException e)
            {
                //  parseIOException(e);
            }
            JObject jsonObject = null;
            try
            {
                if ((jsonString == null) || (jsonString.Length == 0))
                {
                    jsonString = "{}";
                }
                jsonObject = JsonConvert.DeserializeObject<JObject>(jsonString);
            }
            catch (JsonException e)
            {
                //throw new YunJsonException(jsonString, response.code(), e);
            }
            //if (!response.isSuccessful()) {
            //  if (jsonObject.has("result"))
            //  {
            //    String result = jsonObject.optString("result");
            //    if ((!"ok".equals(result.toLowerCase())) && (!"0".equals(result))) {
            //      throw new YunResultException(result, jsonObject.optString("msg"), response.code(), jsonObject);
            //    }
            //  }
            //  else if ((jsonObject.has("code")) && (jsonObject.has("msg")))
            //  {
            //    throw new YunCodeException(jsonString, response.code());
            //  }
            //}
            return jsonObject;
        }

        private static string parseString(HttpWebResponse response,string key)
        {
            string jsonString = string.Empty;
            string result = string.Empty;
            try
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    jsonString = reader.ReadToEnd();
                    LogHelper.Info("response :" + jsonString);
                }
            }
            catch (IOException e)
            {
                //  parseIOException(e);
            }
            JObject jsonObject = null;
            try
            {
                if ((jsonString == null) || (jsonString.Length == 0))
                {
                    return result;
                }
                jsonObject = JsonConvert.DeserializeObject<JObject>(jsonString);
                if (jsonObject[key]!=null)
                {
                    return jsonObject[key].ToString();
                }
            }
            catch (JsonException e)
            {
                //throw new YunJsonException(jsonString, response.code(), e);
            }
            return result;
        }

        private static string parseString(HttpWebResponse response)
        {
            string result = string.Empty;
            try
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                    LogHelper.Info("response :" + result);
                }
            }
            catch (IOException e)
            {
                //  parseIOException(e);
            }
            return result;
        }
        public JObject getJSONObject()
        {
            try
            {
              //  parseXML(_response);
                return parseJSON(_response);
            }
            catch (YunException ex)
            {

                throw;
            }
            return null;
        }

        public string getString()
        {
            try
            {
                //  parseXML(_response);
                return parseString(_response);
            }
            catch (YunException ex)
            {

                throw;
            }
            return null;
        }

        public string getString(string key)
        {
            try
            {
                //  parseXML(_response);
                return parseString(_response,key);
            }
            catch (YunException ex)
            {

                throw;
            }
            return null;
        }
    }
}
